package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonErrorUnitListBean;
import com.fancy.learncenter.bean.CartoonLessonDataBean;
import com.fancy.learncenter.bean.LessonDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.SelectTextBookAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonSelectTextBookActivity extends BaseActivity {

    public final static int SELECT_TEXT_BOOK_CODE = 10002;

    String schoolID = "";

    SelectTextBookAdapter selectTextBookAdapter;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_select_text_book, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("选择教材");

        schoolID = getIntent().getStringExtra(CartoonCreatClassActivity.SCHOOL_ID_KEY);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(linearLayoutManager);


        itemDatas = new ArrayList<>();

        selectTextBookAdapter = new SelectTextBookAdapter(this, itemDatas);
        selectTextBookAdapter.setItemCallBack(new SelectTextBookAdapter.ItemCallBack() {
            @Override
            public void itemCallBack(CartoonLessonDataBean.LessonSimpleVOsBean item) {
                Intent intent = getIntent();
                intent.putExtra(CartoonCreatClassActivity.NAME_KEY, item.getLessonName());
                intent.putExtra(CartoonCreatClassActivity.SCHOOL_ID_KEY, item.getLessonId());
                setResult(SELECT_TEXT_BOOK_CODE, intent);
                finish();
            }
        });

        recyclerView.setAdapter(selectTextBookAdapter);

        getSchool();
    }

    ArrayList<CartoonLessonDataBean.LessonSimpleVOsBean> itemDatas;

    private void getSchool() {

        Map map = new HashMap();
        map.put("getListLessonOfSchool", schoolID);

        HttpMehtod.getInstance().getListLessonOfSchool(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonLessonDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonLessonDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonLessonDataBean>() {
                    @Override
                    public void success(CartoonLessonDataBean dataBean) {
                        selectTextBookAdapter.addAll(dataBean.getLessonSimpleVOs());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }
}
